package net.sxnic.ugr.user.action;

import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * Introduction：修改密码action类
 * 
 * @author 孙宇飞 create date : 2010-5-16
 * @author 吕盛槐 update date : 2011-9-26
 */
@SuppressWarnings("serial")
public class ChangePassword extends UgrActionSupport {

	@Autowired
	private UserManager userManager;

	private String newpassword;

	private String confirmPassword;

	private String oriPassword;

	private String signPwd;

	private String confirmSignPwd;

	private String oriSignPwd;

	private User user;

	public String execute() throws Exception {

		try {
			user = userManager.findByUnique("username",
					AppContext.getUserName(request));

			if (newpassword.equals("")) {
				//addActionMessage("修改后的密码不能为空！");
				return INPUT;
			}
			if (!newpassword.equals(confirmPassword)) {
				addActionMessage("密码和确认密码不相同！");
				return INPUT;
			}

			if (!oriPassword.equals("")
					&& !DigestUtils.md5Hex(oriPassword).equals(
							user.getPassword())) {
				addActionMessage("旧密码错误！");
				return INPUT;
			}

			if (StringUtils.isNotBlank(newpassword)
					|| StringUtils.isNotBlank(this.signPwd)) {

				addLog("修改用户 " + user.getUsername() + " 的密码");
				if (!newpassword.equals("")) {
					user.setPassword(DigestUtils.md5Hex(newpassword));
				}

				userManager.update(user);
				this.addActionMessage("修改密码成功！");
				return INPUT;
			}

			newpassword = "";
			oriPassword = "";
			confirmPassword = "";
			
			// log
			addLog("===用户===" + AppContext.getUserName(request)
					+ "===修改密码成功！===id===" + user.getId());
			// 保存成功信息并转向
			this.addActionMessage("保存成功！");
		} catch (Exception e) {
			this.addActionError("保存失败！");
			e.printStackTrace();
		}

		// 保存成功信息并转向

		return SUCCESS;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	/**
	 * @return the oriSignPassword
	 */
	public String getOriPassword() {
		return oriPassword;
	}

	/**
	 * @param oriSignPassword
	 *            the oriSignPassword to set
	 */
	public void setOriPassword(String oriSignPassword) {
		this.oriPassword = oriSignPassword;
	}

	/**
	 * @return the confirmPassword
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * @param confirmPassword
	 *            the confirmPassword to set
	 */
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getSignPwd() {
		return signPwd;
	}

	public void setSignPwd(String signPwd) {
		this.signPwd = signPwd;
	}

	public String getConfirmSignPwd() {
		return confirmSignPwd;
	}

	public void setConfirmSignPwd(String confirmSignPwd) {
		this.confirmSignPwd = confirmSignPwd;
	}

	public String getOriSignPwd() {
		return oriSignPwd;
	}

	public void setOriSignPwd(String oriSignPwd) {
		this.oriSignPwd = oriSignPwd;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

}
